package com.gl.blank.jsfwebapp;

import javax.faces.context.FacesContext;

import com.gl.blank.jarservices.service.ServiceFactory;
import com.gl.blank.jarservices.service.UserService;

/**
 * Faces bean that initializes application on start up and holds application
 * resources
 */
public class WebApplication
{

    private final ServiceFactory serviceFactory;

    public WebApplication()
    {
        serviceFactory = new ServiceFactory();
    }

    public static UserService getUserService()
    {
        WebApplication webApplication = getBean( "webApplication" );
        return webApplication.serviceFactory.getUserService();
    }

    /**
     * Returns JSF bean
     * 
     * @param beanName
     *            the bean name
     * @return the JSF bean if found
     */
    @SuppressWarnings ( "unchecked")
    public static <T> T getBean( String beanName )
    {
        FacesContext context = FacesContext.getCurrentInstance();
        return (T) context.getApplication().evaluateExpressionGet( context, "#{" + beanName + "}", Object.class );
    }

}
